package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.InvoiceNameBiz;
import com.egao.orderB.entity.InvoiceName;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-10-09 11:21:15
 * @version ：1.0
 */
@Repository
public class InvoiceNameBizImpl extends BaseDao implements InvoiceNameBiz {

	@Override
	public JsonResult list(PageResult2<T> page, InvoiceName invoiceName, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<InvoiceName> rm = new BeanPropertyRowMapper<InvoiceName>(InvoiceName.class);
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.name");
        sql.append(",d.name driverName");
        sql.append(",i.state,i.type,i.remark,i.create_time");
		sql.append(",i.create_user,i.update_time,i.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_invoice_name i ");
        sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
        sql.append(" left join chf_driver d on d.car_no=i.name and i.type='司机' ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id ");
        sql.append(" where i.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and i.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(invoiceName)){
			if(Utils.isNotEmpty(invoiceName.getName())){
				sql.append(" and (instr(i.name,:name)>0 or instr(i.pinyin,:name)>0 ) ");
				map.put("name", invoiceName.getName().toLowerCase());
			} else if(Utils.isNotEmpty(invoiceName.getKeywords())){
				sql.append(" and i.name=:keywords  ");
				map.put("keywords", invoiceName.getKeywords());
			}
			if(Utils.isNotEmpty(invoiceName.getType())){
				sql.append(" and instr(i.type,:type)>0 ");
				map.put("type", invoiceName.getType());
			}
			if(Utils.isNotEmpty(invoiceName.getSort())&&Utils.isNotEmpty(invoiceName.getOrder())){
                sql.append(" order by i."+invoiceName.getSort()+" "+invoiceName.getOrder());
            } else {
                sql.append(" order by i.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<InvoiceName> list(InvoiceName invoiceName, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<InvoiceName> rm = new BeanPropertyRowMapper<InvoiceName>(InvoiceName.class);
        StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.name");
		sql.append(",d.name driverName");
        sql.append(",i.state,i.type,i.remark,i.create_time");
		sql.append(",i.create_user,i.update_time,i.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_invoice_name i ");
        sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" left join chf_driver d on d.car_no=i.name and i.type='司机' ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id ");
        sql.append(" where i.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and i.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(invoiceName)){
			if(Utils.isNotEmpty(invoiceName.getName())){
				sql.append(" and (instr(i.name,:name)>0 or instr(i.pinyin,:name)>0 ) ");
				map.put("name", invoiceName.getName().toLowerCase());
			}
			if(Utils.isNotEmpty(invoiceName.getType())){
				sql.append(" and instr(i.type,:type)>0 ");
				map.put("type", invoiceName.getType());
			}
            if(Utils.isNotEmpty(invoiceName.getSort())&&Utils.isNotEmpty(invoiceName.getOrder())){
				sql.append(" order by i."+invoiceName.getSort()+" "+invoiceName.getOrder());
			} else {
				sql.append(" order by i.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<InvoiceName> getByName(InvoiceName invoiceName) {
		RowMapper<InvoiceName> rm = new BeanPropertyRowMapper<InvoiceName>(InvoiceName.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.name");
		sql.append(",d.name driverName");
		sql.append(",i.state,i.type,i.remark,i.create_time");
		sql.append(",i.create_user,i.update_time,i.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_invoice_name i ");
		sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" left join chf_driver d on d.car_no=i.name and i.type='司机' ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id ");
		sql.append(" where i.deleted=0 and sogzn.deleted=0 and i.name=:name and i.type=:type ");
		map.put("name", invoiceName.getName());
		map.put("type", invoiceName.getType());
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(), map, rm);
	}

	@Override
	public List<InvoiceName> getByName2(InvoiceName invoiceName) {
		RowMapper<InvoiceName> rm = new BeanPropertyRowMapper<InvoiceName>(InvoiceName.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.name");
		sql.append(",i.name driverName");
		sql.append(",i.state,i.type,i.remark,i.create_time");
		sql.append(",i.create_user,i.update_time,i.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_invoice_name i ");
		sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" left join chf_driver d on d.car_no=i.name and i.type='司机' ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id ");
		sql.append(" where i.deleted=0 and sogzn.deleted=0 and i.name=:name and i.type=:type ");
		map.put("name", invoiceName.getName());
		map.put("type", invoiceName.getType());
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(), map, rm);
	}

	@Override
	public InvoiceName getById(InvoiceName invoiceName) {
		RowMapper<InvoiceName> rm = new BeanPropertyRowMapper<InvoiceName>(InvoiceName.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select i.id,i.organization_id,i.name");
		sql.append(",d.name driverName");
        sql.append(",i.state,i.type,i.remark,i.create_time");
		sql.append(",i.create_user,i.update_time,i.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_invoice_name i ");
        sql.append(" inner join sys_user suser on suser.user_id=i.create_user ");
		sql.append(" left join chf_driver d on d.car_no=i.name and i.type='司机' ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=i.organization_id ");
        sql.append(" where i.deleted=0 and sogzn.deleted=0 and i.id=:id ");
		map.put("id", invoiceName.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}


	@Override
	public boolean save(InvoiceName invoiceName, User loginUser) {
		invoiceName.setPinyin(PinyinUtils.ToFirstChar(invoiceName.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_invoice_name (");
        sql.append("organization_id,name,pinyin,state,type");
		sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:pinyin,:state,:type");
		sql.append(",:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", invoiceName.getName());
        map.put("pinyin", invoiceName.getPinyin());
        map.put("state", invoiceName.getState());
        map.put("type", invoiceName.getType());
        map.put("remark", invoiceName.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(InvoiceName invoiceName, User loginUser) {
		invoiceName.setPinyin(PinyinUtils.ToFirstChar(invoiceName.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_invoice_name set ");
        sql.append("name=:name,pinyin=:pinyin,state=:state,type=:type ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", invoiceName.getName());
        map.put("pinyin", invoiceName.getPinyin());
        map.put("state", invoiceName.getState());
        map.put("type", invoiceName.getType());
        map.put("remark", invoiceName.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", invoiceName.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
	@Override
	public boolean updateState(InvoiceName invoiceName, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_invoice_name set ");
        sql.append("state=:state ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("state", invoiceName.getState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", invoiceName.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(InvoiceName invoiceName, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(invoiceName.getId().length()>0){
			for(String ID : invoiceName.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_invoice_name set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
